/**
 * Created by duwei on 2018/6/4.
 */
var RecordDetailCellItem = Item.extend({
    _ccsFileName:res.RecordDetailCellUnit_json,
    /**
     * 初始化
     */
    initItem:function () {
        this._initRecordDetailCellUnit();
    },
    /**
     * 初始化界面
     * @private
     */
    _initRecordDetailCellUnit: function () {
        var self = this;
        self.nodeRoot = self._nodeList['root'];
        self.startPosx = self.nodeRoot["_lb_score1"].getPositionX();
        self.endPosx = self.nodeRoot["_lb_score4"].getPositionX();
        self.startPosx2 = (self.nodeRoot["_lb_score1"].getPositionX() + self.nodeRoot["_lb_score2"].getPositionX()) / 2;
        self.endPosx2 = (self.nodeRoot["_lb_score3"].getPositionX() + self.nodeRoot["_lb_score4"].getPositionX()) / 2;

        TouchUtil.setOnclickListener(self.nodeRoot['_btn_huifang'], function () {
            if (self.data) {
                showLoading("正在加载战绩...");
                self.reTry();
            }
        });
        TouchUtil.setOnclickListener(self.nodeRoot['_btn_fenxiang'], function () {
            if (self.data) {
                var playbackId = '' + self.data["_id"] + (self.index + 1);
                console.log(playbackId);
                ShareUtil.SharePlaybackId(playbackId);
            }
        });
    },
    /**
     * 刷新数据
     * @param index
     * @param data
     */
    refreshData: function (index, data) {
        var self = this;
        self.index = index;
        self.data = data;
        self.playerCount = 9;
        var Count = 0;
        var playerMap = [];
        for (var i = 1; i <= self.playerCount; i++) {
            if (self.data["nickname" + i]) {
                playerMap.push(i);
                Count++
            }
        }
        var startPosx = self.startPosx;
        var endPosx = self.endPosx;
        if (Count < 4) {
            startPosx = self.startPosx2;
            endPosx = self.endPosx2;
        }
        self.interval = (endPosx - startPosx) / (Count - 1);
        self.result = self.data['result_arr'][index];
        self.recordCode = self.data["_id"] + "-" + (index + 1);
        self.replayurl = app.recordPath[1] + "/"+app.recordPath[2] + self.recordCode;
        self.nodeRoot["_lb_id"].setString(self.index + 1);
        var isJiesan = false;
        if(self.result[0] == -1){
            self.nodeRoot["_time1"].setString('解散状态');
            self.nodeRoot["_time2"].setString('');
            isJiesan = true;
        }else{
            var timeArr = timestamp2time(self.result[0]).split(' ');
            self.nodeRoot["_time1"].setString(timeArr[0]);
            self.nodeRoot["_time2"].setString(timeArr[1]);
        }
        self.nodeRoot['_btn_huifang'].setVisible(!isJiesan);
        self.nodeRoot['_btn_fenxiang'].setVisible(!isJiesan);
        self.record_round = index + 1;
        //房主
        var fangzhuIndex = 0;
        for (var i = 1; i <= self.playerCount; i++) {
            var idx = playerMap[i-1];
            //谁是房主
            if (self.data["uid" + idx] && self.data.owner_uid == self.data["uid" + idx]) {
                fangzhuIndex = idx;
                break;
            }
        }

        for (var i = 1; i <= self.playerCount; i++) {
            if(isJiesan){
                var idx = playerMap[i-1];
                var lb_score = self.nodeRoot["_lb_score"+i];
                if(self.data["nickname" + idx]){
                    var score = (self.result[idx] == undefined)?0:self.result[idx];
                    lb_score.setVisible(true);
                    lb_score.setString(score>0?'已同意':'未操作');
                    lb_score.setPositionX(startPosx + self.interval*(i-1));
                    lb_score.setTextColor(score>0?cc.color("#BC6F00"):cc.color("#43873A"));
                }else if(lb_score){
                    lb_score.setVisible(false);
                }
            }else{
                var idx = playerMap[i-1];
                var lb_score = self.nodeRoot["_lb_score"+i];
                if(self.data["nickname" + idx]){
                    var score = (self.result[idx] == undefined)?0:self.result[idx];
                    lb_score.setVisible(true);
                    lb_score.setString(score);
                    lb_score.setPositionX(startPosx + self.interval*(i-1));
                    lb_score.setTextColor(score>=0?cc.color("#BC6F00"):cc.color("#43873A"));
                }else if(lb_score){
                    lb_score.setVisible(false);
                }
            }
        }
    },
    reTry: function () {
        var self = this;
        cc.log(self.replayurl);
        showLoading('正在解析回放资源...');
        http.get(self.replayurl, function (data) {
            var tData = data.replace(/'/g, "\"");
            data = JSON.parse(tData);
            if (data['3002']) {
                gameData.mapId = data['3002']['data']['map_id'];
            } else {
                var option = JSON.parse(data[0]['data']['Option']);
                gameData.mapId = option['mapid'];
            }
            gameData.recordIds = self.recordCode.split('-');
            SubUpdateUtil.checkUpdateSubByMapId(gameData.mapId,function (sub) {
                showRoom(data, ROOM_DATA_TYPE.REPLAY);
            });
        }, function () {
            hideLoading();
            showAlert('请求信息失败,请检查您的网络');
        });
    }
});